﻿using System;
using System.Data;
using System.Data.Common;
using YiYiTong.Rocket.Entity;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.DataAccess
{
    public class GoodsDataAccess
    {
        #region 单例模式
        private GoodsDataAccess()
        {
        }

        private static GoodsDataAccess _instance;
        private static readonly object _syncObject = new object();

        public static GoodsDataAccess Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (_syncObject)
                    {
                        if (_instance == null)
                        {
                            _instance = new GoodsDataAccess();
                        }
                    }
                }
                return _instance;
            }
        }
        #endregion 单例模式

        /// <summary>
        /// 品牌列表
        /// </summary>
        /// <returns></returns>
        public DataSet GetBrandList(long brand_category, string brand_name, string brand_name_en, long category_id, int pageIndex, int pageRows)
        {
            DataSet ds = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_brand_getlist";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@brand_category", DbType.Int64, brand_category);
                db.AddInParameter(Procdbcomm, "@category_id", DbType.Int64, category_id);
                db.AddInParameter(Procdbcomm, "@brand_name", DbType.String, brand_name);
                db.AddInParameter(Procdbcomm, "@brand_name_en", DbType.String, brand_name_en);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("品牌列表-GetBrandList" + ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 商品分类
        /// </summary>
        /// <returns></returns>
        public DataTable GetCategory(long brand_id)
        {
            DataTable dt = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "proc_category_getall";
                DbCommand Procdbcomm = db.GetStoredProcCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@brand_id", DbType.Int64, brand_id);
                var ds = db.ExecuteDataSet(Procdbcomm);

                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("商品分类-GetGoodsCategory" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 商品分类
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllCategory()
        {
            DataTable dt = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "select * from ADZ_GoodsCategory where is_show=1 and is_del=0 order by sort_NO desc";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                var ds = db.ExecuteDataSet(Procdbcomm);

                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("商品分类-GetGoodsCategory" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 生成商品分类的编码
        /// level 分层的级别,顶级分类（根分类）为1，二级分类为2，依次类推
        /// </summary>
        public int BuildCategoryCode(long category_id, int level, out string code)
        {
            int ret = 0;
            code = "";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_category_code_create";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@category_id", DbType.Int64, category_id);
                db.AddInParameter(Procdbcomm, "@level", DbType.Int32, level);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int32, 8);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                ret = (int)db.GetParameterValue(Procdbcomm, "@ret");
                code = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("生成商品分类的编码-BuildCategoryCode" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 商品查询
        /// </summary>
        /// <param name="label_type">1 直播 2 预热 3 爆款</param>
        /// <param name="brand_id">品牌id</param>
        /// <param name="category_id">分类id</param>
        /// <param name="selKey">搜索关键字</param>
        /// <param name="pageIndex"></param>
        /// <param name="pageRows"></param>
        /// <returns></returns>
        public DataSet GetGoodsList(long uid, int label_type, long brand_id, long category_id, string selKey, int pageIndex, int pageRows,int is_withdraw, long supplier_id, int base_label_type)
        {
            DataSet ds = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_goods_getlist";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@label_type", DbType.Int16, label_type);
                db.AddInParameter(Procdbcomm, "@brand_id", DbType.Int64, brand_id);
                db.AddInParameter(Procdbcomm, "@category_id", DbType.Int64, category_id);
                db.AddInParameter(Procdbcomm, "@MPN", DbType.String, selKey);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                db.AddInParameter(Procdbcomm, "@is_withdraw", DbType.Int32, is_withdraw);
                db.AddInParameter(Procdbcomm, "@supplier_id", DbType.Int64, supplier_id);
                db.AddInParameter(Procdbcomm, "@base_label_type", DbType.Int64, base_label_type);
                
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("商品查询-GetGoodsList" + ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 获取批量转发商品列表
        /// </summary>
        /// <param name="label_type">1 直播 2 预热 3 爆款</param>
        /// <param name="brand_id">品牌id</param>
        /// <param name="category_id">分类id</param>
        /// <param name="selKey">搜索关键字</param>
        /// <returns></returns>
        public DataSet GetGoodsBacthList(long uid, int label_type, long brand_id, long category_id, string selKey, int pageIndex, int pageRows,int base_label_type)
        {
            DataSet ds = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_goods_getbatchlist";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@label_type", DbType.Int16, label_type);
                db.AddInParameter(Procdbcomm, "@brand_id", DbType.Int64, brand_id);
                db.AddInParameter(Procdbcomm, "@category_id", DbType.Int64, category_id);
                db.AddInParameter(Procdbcomm, "@MPN", DbType.String, selKey);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                db.AddInParameter(Procdbcomm, "@base_label_type", DbType.Int32, base_label_type);
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取批量转发商品列表-GetGoodsBacthList" + ex.ToString());
            }
            return ds;
        }

        /// <summary>
        /// 获取商品图片
        /// </summary>
        /// <param name="goods_id"></param>
        /// <returns></returns>
        public DataTable GetGoodsIMG(long goods_id, bool is_one = false)
        {
            DataTable dt = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "select * from ADZ_GoodsIMG where goods_id=@goods_id order by sort_NO,seq_id ";
                if (is_one)
                    sqlName = "select top 1 * from ADZ_GoodsIMG where goods_id=@goods_id order by sort_NO,seq_id ";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@goods_id", DbType.Int64, goods_id);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取商品图片-GetGoodsIMG" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 获取商品
        /// </summary>
        /// <param name="goods_id"></param>
        /// <returns></returns>
        public DataTable GetGoods(long goods_id)
        {
            DataTable dt = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "select * from Views_Goods where goods_id=@goods_id and is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@goods_id", DbType.Int64, goods_id);
                var ds = db.ExecuteDataSet(Procdbcomm);
                if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
                    dt = ds.Tables[0];
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("获取商品-GetGoods" + ex.ToString());
            }
            return dt;
        }

        /// <summary>
        /// 扣除库存
        /// </summary>
        /// <param name="goods_id">商品id</param>
        /// <param name="amount">购买数量</param>
        /// <returns></returns>
        public int DeductStockAmount(long goods_id, decimal amount)
        {
            int ret = 0;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "UPDATE ADZ_GoodsMaster SET stock_amount -=@amount WHERE goods_id=@goods_id and is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@goods_id", DbType.Int64, goods_id);
                db.AddInParameter(Procdbcomm, "@amount", DbType.Decimal, amount);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("扣除库存-DeductStockAmount" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 增加库存
        /// </summary>
        public int AddStockAmount(long goods_id, decimal amount)
        {
            int ret = 0;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string sqlName = "UPDATE ADZ_GoodsMaster SET stock_amount +=@amount WHERE goods_id=@goods_id and is_del=0";
                DbCommand Procdbcomm = db.GetSqlStringCommand(sqlName);
                db.AddInParameter(Procdbcomm, "@goods_id", DbType.Int64, goods_id);
                db.AddInParameter(Procdbcomm, "@amount", DbType.Decimal, amount);
                ret = db.ExecuteNonQuery(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("增加库存-AddStockAmount" + ex.ToString());
                ret = -99;
            }
            return ret;
        }

        /// <summary>
        /// 商品转发记录
        /// </summary>
        /// <param name="goods_id"></param>
        /// <param name="uid"></param>
        /// <param name="device_type"></param>
        /// <param name="remark"></param>
        /// <param name="stroutput"></param>
        /// <returns></returns>
        public long Forward(long goods_id, long uid, int device_type, string batch, string remark, out string stroutput)
        {
            stroutput = "记录失败";
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_goodsforward_add";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@goods_id", DbType.Int64, goods_id);
                db.AddInParameter(Procdbcomm, "@uid", DbType.Int64, uid);
                db.AddInParameter(Procdbcomm, "@device_type", DbType.Int32, device_type);
                db.AddInParameter(Procdbcomm, "@batch", DbType.String, batch);
                db.AddInParameter(Procdbcomm, "@remark", DbType.String, remark);
                db.AddOutParameter(Procdbcomm, "@ret", DbType.Int64, 18);
                db.AddInOutParameter(Procdbcomm, "@strOutPut", 200);
                db.ExecuteNonQuery(Procdbcomm);

                long ret = (long)db.GetParameterValue(Procdbcomm, "@ret");
                stroutput = (string)db.GetParameterValue(Procdbcomm, "@strOutPut");
                return ret;
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("商品转发记录-Forward" + ex.ToString());
                return -99;
            }
        }

        /// <summary>
        /// 商品转发统计
        /// </summary>
        /// <returns></returns>
        public DataSet ForwardTongJi(long supplier_id, long brand_id,long category_id,string beg_date,string end_date, int pageIndex, int pageRows)
        {
            DataSet ds = null;
            try
            {
                DBHelper db = DBFactory.ApiDB;
                string storeName = "proc_goodsforward_tongji";
                DbCommand Procdbcomm = db.GetStoredProcCommand(storeName);
                db.AddInParameter(Procdbcomm, "@supplier_id", DbType.Int64, supplier_id);
                db.AddInParameter(Procdbcomm, "@brand_id", DbType.Int64, brand_id);
                db.AddInParameter(Procdbcomm, "@category_id", DbType.Int64, category_id);
                db.AddInParameter(Procdbcomm, "@beg_date", DbType.String, beg_date);
                db.AddInParameter(Procdbcomm, "@end_date", DbType.String, end_date);
                db.AddInParameter(Procdbcomm, "@pageIndex", DbType.Int32, pageIndex);
                db.AddInParameter(Procdbcomm, "@pageRows", DbType.Int32, pageRows);
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                //记录日志
                LogHelper.Error("商品转发统计-ForwardTongJi" + ex.ToString());
            }
            return ds;
        }


        public DataSet GetXQBOrderGoodsList(string sql)
        {
            DataSet ds = new DataSet();
            try
            {
                DBHelper db = DBFactory.ApiDB;
                DbCommand Procdbcomm = db.GetSqlStringCommand(sql);
                ds = db.ExecuteDataSet(Procdbcomm);
            }
            catch (Exception ex)
            {
                LogHelper.Error("根据条件筛选后台用户-GetPageList" + ex.ToString());
            }
            return ds;
        }
    }
}
